/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.util;

import com.eng.scc.core.view.ApplicationContext;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Rafael
 */
public class HTMLFactory
{
    private static DateFormat df = new SimpleDateFormat( "dd/MM/yyyy" );
    
    public static String NOT_AVAIBLE = "n/d";
    
    
    public static interface Content
    {
        public String generateContext();
    }
    
    public static class Body
    {

        public Body()
        {
        }
        
        public static Body EMPTY = new Body();
        
        private List<Content> contents = new ArrayList<Content>();
        private String cssUrl = "";
        
        public void setUrl( String url )
        {
            cssUrl = url;
        }
        
        public void addContent( Content content )
        {
            if ( content == null )
            {
                throw new IllegalArgumentException( "Content HTML cannot be null!" );
            }
            
            this.contents.add( content );
        }

        @Override
        public String toString()
        {
            StringBuilder sb = new StringBuilder();
            sb.append( "<html>" );
            sb.append( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" );
            sb.append( "<head>" );
            sb.append( "<link rel=\"stylesheet\" type=\"text/css\" href=\"" );
            sb.append( cssUrl );
            sb.append( "\"/>" );
            sb.append( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" );
            sb.append( "</head>" );
            
            for ( Content content : contents )
            {
                sb.append( content.generateContext() );            
            }
            
            sb.append( "</html> " );

            /*Must be called to build all structure HTML*/
            return sb.toString();
        }
    }
    
    public static class Title implements Content
    {
        String title = "Title";
        
        public Title( String title )
        {
            this.title = title;
        }
        
        @Override
        public String generateContext()
        {
            return "<h1>" + title + "</h1>";
        }
    }
    
    /**
     *
     */
    public static class TableDetails implements Content
    {
        private List<String[]> content = new ArrayList<String[]>();

        public void addItem( String property, String value )
        {
            addItem( property, value, false );
        }
        
        public void addItem( String property, String value, boolean masked )
        {
            if ( value != null && masked )
            {
                value = value.replaceAll( "\\D", "" ).trim().isEmpty() ? value.replaceAll( "\\D", "" ).trim() : value;
            }
            
            content.add( new String[]
            {
                property + ":",
                value != null && ! value.isEmpty() ? value : NOT_AVAIBLE
            });
        }
        
        public void addItem( String property, int value )
        {
            content.add( new String[]
            {
                property + ":",
                String.valueOf( value )
            });
        }
        
        public void addItem( String property, Date value )
        {
            addItem( property, value, false );
        }
        
        public void addItem( String property, Date value, boolean timeStamp )
        {
            String v = NOT_AVAIBLE;
            
            if ( value != null )
            {
                if ( timeStamp )
                {
                    DateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy HH:mm:ss" );

                    v = dateFormat.format( value );
                }
                else
                {
                    v = df.format( value );
                }
            }
             
            content.add( new String[]
            {
                property + ":",
                v  
            });
        }
        
        public void addItem( String property, double value )
        {
            try
            {
                NumberFormat valueFormat = NumberFormat.getInstance();
                {
                    valueFormat.setMinimumFractionDigits( 2 );
                    valueFormat.setMaximumFractionDigits( 2 );
                }
                
                content.add( new String[]
                {
                    property + ":",
                    valueFormat.format( value )
                });
            }
        
            catch ( Exception e )
            {
                ApplicationContext.getInstance().saveLog( e );
            }
        }
        
        public String generateContext()
        {
            StringBuilder sb = new StringBuilder();
            
            if ( ! content.isEmpty() )
            {
                sb.append( "<table class=\"table_caption\" height=\"100%\" width=\"98%\" >" );
                
                for ( String[] value : content )
                {
                    sb.append( "<tr>" );
                    sb.append( "<td class=\"title_arg\" width=\"40%\">" + value[0] + "</td>" );
                    sb.append( "<td class=\"content_arg\">" + value[1] + "</td>" );
                    sb.append( "</tr>" );
                }
                
                sb.append( "</table>" );
            }
                    
            return sb.toString();
        }
    }
    
    public static class Table implements Content
    {

        public Table()
        {
        }
        
        public Table( String[][] header )
        {
            headerNamesWidths = header;
        }
        
//        name, width example
//        String[][] array = 
//        {
//            {
//                "name", 
//                "width"
//            },
//        };
        private String[][] headerNamesWidths = null;
        private List<String[]> data = new ArrayList<String[]>();

        public String[][] getHeaderNamesWidths()
        {
            return headerNamesWidths;
        }

        public void setHeaderNamesWidths( String[][] headerNamesWidths )
        {
            this.headerNamesWidths = headerNamesWidths;
        }

        public List<String[]> getData()
        {
            return data;
        }

        public void setData( List<String[]> data )
        {
            this.data.clear();
            this.data.addAll( data );
        }
        
        @Override
        public String generateContext()
        {
            if ( headerNamesWidths == null )
            {
                throw new IllegalArgumentException( "headerNamesWidths cannot be null" );
            }
            
            if ( data == null )
            {
                throw new IllegalArgumentException( "headerNamesWidths cannot be null" );
            }
            
            StringBuilder sb = new StringBuilder();
            
            sb.append( "<table class=\"table_caption\" height=\"100%\" width=\"98%\" >" );
            sb.append( "<tr>" );
            
            for ( String[] value : headerNamesWidths )
            {
                String name = value[0];
                String width = value[1];
                
                sb.append( "<th class=\"tableHeader\" width=\"" + width + "\">" + name + "</th>" );
            }
            
            sb.append( "</tr>" );
            
            
            for ( String[] obj : data )
            {
                sb.append( "<tr>" );

                for ( String s : obj )
                {
                    sb.append( "<td class=\"tableContent\" height=\"20px\" >" );
                    sb.append( s );
                    sb.append( "</td>" );
                }
                
                sb.append( "</tr>" );
            }
            
            sb.append( "</table>" );
            
            return sb.toString();
        }
    }
    
    public static void main( String[] args )
    {
        String[][] array = 
        {
            {
                "12", 
                "15"
            },
        };
        
        for ( String[] strings : array )
        {
            System.out.println( strings[0] + "+" + strings[1] );
        }
    }
}
